#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

# Author: jiyin@redhat.com
# Used to delete specified test run.

lappend ::auto_path $::env(HOME)/lib /usr/local/lib /usr/lib64 /usr/lib
package require getOpt 3.0
package require runtestlib 1.1
package require tdom
package require sqlite3
package require md5
namespace import ::getOpt::* ::runtestlib::*

# global var
array set Opt {}
array set InvalidOpt {}
set Args [list]
set OptionList {
  "*Options:" {
	{help h}     {arg n	help {Print this usage}}
	y            {arg n	help {yes}}
	{a aborted}  {arg n	help {only delete the "(aborted)" jobs}}
	{n nil}      {arg n	help {only delete the "(nil)" jobs}}
	delpkg       {arg n	help {delete all tests that along the pkg and all related run info}}
  }
}
set deltype run

proc Usage {progname} {
	puts "Usage: $progname <pkgname> <autotest name> \[options\]"
	puts "Usage: $progname <pkgname> -delpkg"

	getUsage $::OptionList
	puts "Info: You can use bkr-autorun-stat --lsrun to list all autotest name"
}

# _parse_ argument
getOptions $OptionList $::argv Opt InvalidOpt Args
puts "Debug: Args{$Args}"
parray InvalidOpt
parray Opt

if [info exist Opt(help)] {
	Usage $argv0
	exit 0
}

if {[llength $Args] < 1} {
	Usage $argv0
	exit 1
}

set pkgName {}
if [info exist Opt(delpkg)] {
	set deltype pkg
}

if {$deltype == "run" && [llength $Args] < 2} {
	Usage $argv0
	exit 1
}

# Get test package name and autorun name
set pkgName [lindex $Args 0]
set Distro_Gset [lindex $Args 1]

if [info exists Opt(n)] {
	set toDel "(nil)"
} elseif [info exists Opt(a)] {
	set toDel "(aborted)"
} else {
	set toDel "All"
}

if [info exists Opt(y)] {
	set ans {y}
} else {
	puts "Will Delete *${toDel}* Jobs of {${Distro_Gset}}/$pkgName"
	puts -nonewline "***(Are you sure)\[y/n\] "
	flush stdout
	gets stdin ans
}

# __main__
cd [dbroot]
sqlite3 db testrun.db
db timeout 6000
if {[string match -nocase y* $ans]} {
	if {$deltype == "run"} {
	    if {$toDel == "All"} {
		db transaction {
			db eval "DELETE FROM testrun
				WHERE distro_rgset = '$Distro_Gset' and testid in (
					select testid
					from testinfo
					where testrun.testid = testinfo.testid and testinfo.pkgName = '$pkgName'
				)
			"
		}
		#exec "rm -rf log/[string map {{ } _} $Distro_Gset]"
	    } elseif {$toDel == "(nil)"} {
		db transaction {
			db eval "DELETE FROM testrun
				WHERE testrun.testStat = '' and distro_rgset = '$Distro_Gset' and testid in (
					select testid
					from testinfo
					where testrun.testid = testinfo.testid and testinfo.pkgName = '$pkgName'
				)
			"
		}
	    } elseif {$toDel == "(aborted)"} {
		db transaction {
			db eval "DELETE FROM testrun
				WHERE testrun.testStat = 'aborted' and distro_rgset = '$Distro_Gset' and testid in (
					select testid
					from testinfo
					where testrun.testid = testinfo.testid and testinfo.pkgName = '$pkgName'
				)
			"
		}
	    }
	}
	if {$deltype == "pkg"} {
		db transaction {
			db eval "DELETE FROM testrun
				WHERE testid in (
					select testid
					from testinfo
					where testinfo.pkgName = '$pkgName'
				)
			"
			db eval "DELETE FROM testinfo
				WHERE pkgName = '$pkgName'
			"
		}
	}
}

